/************************************************************************
  			hu/myai/tick/AbstractTickProvider.java - Copyright bozbalint

Here you can write a license for your code, some comments or any other
information you want to have in your generated code. To to this simply
configure the "headings" directory in uml to point to a directory
where you have your heading files.

or you can just replace the contents of this file with your own.
If you want to do this, this file is located at

/opt/kde3/share/apps/umbrello/headings/heading.java

-->Code Generators searches for heading files based on the file extension
   i.e. it will look for a file name ending in ".h" to include in C++ header
   files, and for a file name ending in ".java" to include in all generated
   java code.
   If you name the file "heading.<extension>", Code Generator will always
   choose this file even if there are other files with the same extension in the
   directory. If you name the file something else, it must be the only one with that
   extension in the directory to guarantee that Code Generator will choose it.

you can use variables in your heading files which are replaced at generation
time. possible variables are : author, date, time, filename and filepath.
just write %variable_name%

This file was generated on Sat Mar 14 2009 at 12:14:34
The original location of this file is /home/bozbalint/uml-generated-code/hu/myai/tick/AbstractTickProvider.java
 **************************************************************************/

package hu.myai.api.tick;

import hu.myai.model.TickVO;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Class AbstractTickProvider
 *
 * @author bozbalint
 *
 */
/**
 * @author u299863
 * 
 */
abstract public class AbstractTickProvider implements TickProviderInterface {

	/**
	 * The object used when logging debug,errors,warnings and info.
	 * */
	private final Logger log = LoggerFactory.getLogger(this.getClass());

	/**
	 * Name of the consumer
	 */
	protected String name;

	/**
	 * generate final timestamp for the scaled tick
	 * */
	private int tickCounter = 0;

	/**
	 * it is for merge ticks. timeScale represent the number of tick is merged
	 * to one tick. Default is 1. That means no merge.
	 * */
	protected int timeScale = 1;

	/**
	 * list of registered indicators
	 */
	private List<TickConsumerInterface> tickConsumerList = new ArrayList<TickConsumerInterface>();

	/**
	 * Constructor
	 */
	public AbstractTickProvider(String name) {
		this.name = name;
	};

	/**
	 * @return the value of name
	 */
	public String getName() {
		return name;
	}

	public void setTimeScale(int timeScale) {
		this.timeScale = timeScale;
	}

	public int getTimeScale() {
		return timeScale;
	}

	public boolean addTickConsumer(TickConsumerInterface tickConsumer) {
		tickConsumerList.add(tickConsumer);
		return false;
	}

	public boolean removeTickConsumer(TickConsumerInterface tickConsumer) {
		tickConsumerList.remove(tickConsumer);
		return false;
	}

	/**
	 * backend feature to check the registered listeners
	 */
	public List<TickConsumerInterface> getTickConsumerList() {
		return tickConsumerList;
	}

	protected TickVO tickCollection;

	/**
	 * apply the time scale on the tick than publish the tick to all registered
	 * indicators
	 * */
	protected void propegateTick(TickVO tick) {
		// init the first
		if (tickCollection == null) {
			tickCollection = tick.clone();
			return;
		}

		// merge tick to tickCollection
		if (tickCollection.getHigh() < tick.getHigh())
			tickCollection.setHigh(tick.getHigh());
		if (tickCollection.getLow() > tick.getLow())
			tickCollection.setLow(tick.getLow());

		// publish tickCollection, milis in sec = 1000 * 60, this not
		boolean isLast = tick.getTime().getTime() % (timeScale * 1000 * 60) == (timeScale - 1) * 1000 * 60;
		if (isLast) {
			// if (tick.getTimestamp() % timeScale == timeScale - 1) {
			// finish the merge
			tickCollection.setClose(tick.getClose());
			tickCollection.setTimeScale(timeScale);
			tickCollection.setTimestamp(tickCounter++);

			log.debug("scale=" + tickCollection.toString());

			for (TickConsumerInterface tickConsumer : tickConsumerList) {
				tickConsumer.addTick(tickCollection);
			}
			tickCollection = null;
		}
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer("[" + name + " Tick]\n");
		sb.append("Registered Indicators: ");
		for (TickConsumerInterface tickConsumer : tickConsumerList) {
			sb.append("\n\t " + tickConsumer.getName());
		}

		return sb.toString();
	}
}
